package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.service.wx.WxSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public List<String> getAssociativeKeyword(String keyword) {
        // 获取关联的关键字
        MarketKeywordExample example = new MarketKeywordExample();
        example.createCriteria().andKeywordLike("%" + keyword + "%").andDeletedEqualTo(false);
        List<MarketKeyword> keywords = keywordMapper.selectByExample(example);

        // 获取关联的商品名
        MarketGoodsExample example1 = new MarketGoodsExample();
        example1.createCriteria().andNameLike("%" + keyword + "%").andDeletedEqualTo(false);
        List<MarketGoods> goodsList = goodsMapper.selectByExample(example1);

        // 构建返回值，封装
        List<String> list = new ArrayList<>();
        for (MarketKeyword keyWord : keywords) {
            list.add(keyWord.getKeyword());
        }
        for (MarketGoods goods : goodsList) {
            list.add(goods.getName());
        }

        return list;
    }

    @Override
    public void clearHistory(Integer userId) {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        example.or().andUserIdEqualTo(userId);
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setUpdateTime(LocalDateTime.now());
        marketSearchHistory.setDeleted(true);
        searchHistoryMapper.updateByExampleSelective(marketSearchHistory,example);
    }

    @Override
    public void add(MarketSearchHistory marketSearchHistory) {
        // 获取到关键字
        String keyword = marketSearchHistory.getKeyword();
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        example.createCriteria().andKeywordEqualTo(keyword).andUserIdEqualTo(marketSearchHistory.getId());
        MarketSearchHistory searchHistory = searchHistoryMapper.selectOneByExample(example);
        // 如果不存在，插入
        if (searchHistory == null){
            searchHistoryMapper.insertSelective(marketSearchHistory);
            return;
        }
        // 更新搜索的时间
        searchHistory.setUpdateTime(marketSearchHistory.getUpdateTime());
        // 若是被逻辑删除，恢复
        if (searchHistory.getDeleted()){
            searchHistory.setDeleted(false);
        }
        searchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
    }

    @Override
    public Map<String, Object> searchIndex(Integer userId) {
        Map<String,Object> result = new HashMap<>();
        // 查询默认关键字
        MarketKeywordExample defaultKeywordExample = new MarketKeywordExample();
        defaultKeywordExample.or().andDeletedEqualTo(false).andIsDefaultEqualTo(true);
        MarketKeyword defaultKeyword = keywordMapper.selectOneByExample(defaultKeywordExample);
        result.put("defaultKeyword",defaultKeyword);

        // hotKeywordList
        MarketKeywordExample hotKeywordExample = new MarketKeywordExample();
        hotKeywordExample.or().andDeletedEqualTo(false).andIsHotEqualTo(true);
        List<MarketKeyword> hotKeywordList = keywordMapper.selectByExample(hotKeywordExample);

        List<Map<String,String>> historyKeywordList = new ArrayList<>();
        result.put("hotKeywordList",hotKeywordList);

        if (userId != null) {
            MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
            historyExample.or().andDeletedEqualTo(false).andUserIdEqualTo(userId);
            List<MarketSearchHistory> historyList = searchHistoryMapper.selectByExample(historyExample);
            for (MarketSearchHistory history : historyList) {
                Map<String,String> keyword = new HashMap<>();
                keyword.put("keyword", history.getKeyword());
                historyKeywordList.add(keyword);
            }
        }

        result.put("historyKeywordList",historyKeywordList);

        return result;
    }
}
